package thesis.bean;

import java.io.IOException;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.component.behavior.AjaxBehavior;
import javax.faces.context.FacesContext;
import javax.faces.event.AjaxBehaviorEvent;
import javax.faces.event.ValueChangeEvent;
import javax.faces.validator.ValidatorException;
import javax.xml.bind.ValidationException;

import com.sun.org.apache.bcel.internal.generic.NEW;

import thesis.database.Taxi;
import thesis.javaclass.SystemModel;
import thesis.servlets.*;

@ManagedBean(name = "taxi")
@SessionScoped
public class TaxiBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String id;
	private Taxi taxi = new Taxi();
	private List<Taxi> taxies = SystemModel.getAllTaxies();
	private boolean hasTaxi = true;
	private String number;

	public TaxiBean() {

	}

	public boolean isHasTaxi() {
		return hasTaxi;
	}

	public void setHasTaxi(boolean hasTaxi) {
		this.hasTaxi = hasTaxi;
	}

	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	public String getId() {
		return id;
	}

	public void setTaxi(Taxi taxi) {
		this.taxi = taxi;
	}

	public Taxi getTaxi() {
		return taxi;
	}

	public List<Taxi> getTaxies() {
		return taxies;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void editTaxi() {
		if (taxi == null)
			return;

		try {
			FacesContext.getCurrentInstance().getExternalContext()
					.redirect("edit-taxi.jsf");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public String updateTaxi() throws UnsupportedEncodingException {
		taxi.setLastUpdate(new Date());
		boolean ok = SystemModel.updateTaxi(this.taxi);

		FacesContext context = FacesContext.getCurrentInstance();
		context.getExternalContext().getFlash().setKeepMessages(true);
		if (ok)
			context.addMessage(null,
					new FacesMessage("Edited taxi " + taxi.getTaxiId()
							+ " successfully"));
		else
			context.addMessage(null,
					new FacesMessage("Edited taxi " + taxi.getTaxiId()
							+ " failing"));

		this.taxi = new Taxi();
		valueChanged();
		return "taxi?faces-redirect=true";
	}

	public void addTaxiClick() {
		taxi = new Taxi();
		try {
			FacesContext.getCurrentInstance().getExternalContext()
					.redirect("add-taxi.jsf");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public String addTaxi() {
		boolean ok = SystemModel.addTaxi(this.taxi);

		FacesContext context = FacesContext.getCurrentInstance();
		context.getExternalContext().getFlash().setKeepMessages(true);
		if (ok)
			context.addMessage(null,
					new FacesMessage("Added taxi successfully"));
		else
			context.addMessage(null, new FacesMessage("Added taxi failing"));

		this.taxi = new Taxi();
		valueChanged();
		return "taxi?faces-redirect=true";
	}

	public void deleteTaxi() {
		/*
		 * FacesContext context = FacesContext.getCurrentInstance(); int id =
		 * getIdCode(context);
		 */

		if (taxi == null)
			return;

		FacesContext context = FacesContext.getCurrentInstance();
		boolean ok = SystemModel.deleteTaxi(taxi.getTaxiId());
		if (ok) {
			context.addMessage(null,
					new FacesMessage("Deleted taxi " + taxi.getTaxiId()
							+ " successfully"));
			valueChanged();
		} else {
			context.addMessage(null,
					new FacesMessage("Deleted Fail " + taxi.getTaxiId()
							+ " Failing!"));
		}
	}

	public int getIdCode(FacesContext context) {
		Map<String, String> params = context.getExternalContext()
				.getRequestParameterMap();

		return Integer.parseInt(params.get("id"));
	}

	public void valueChanged() {
		if (number == null || number == "") {
			taxies = SystemModel.getAllTaxies();
		} else {
			taxies = SystemModel.getTaxiByNumberPlate(number);
		}

		if (taxies != null && taxies.size() != 0) {
			hasTaxi = true;
		} else
			hasTaxi = false;

	}

	public void validateImei(FacesContext fc, UIComponent c, Object value) {
		String temp = (String) value;
		if (!(temp.matches("[0-9]+") && temp.length() == 15))
			throw new ValidatorException(new FacesMessage(
					"Imei must have 15 numbers"));
	}

	public void validateSeat(FacesContext fc, UIComponent c, Object value) {
		String temp = value.toString();
		if (!(temp.matches("[4]{1}") || temp.matches("[7]{1}")))
			throw new ValidatorException(new FacesMessage(
					"Number of Seats must between 4 or 7"));
	}

	public void validateNumberPlate(FacesContext fc, UIComponent c, Object value) {
		if (!(((String) value).length() >= 8))
			throw new ValidatorException(new FacesMessage(
					"Number Plate must has at least 8 characters"));
	}

	public void validateSim(FacesContext fc, UIComponent c, Object value) {
		String temp = value.toString();
		if (!(temp.matches("[0-9]+") && temp.length() >= 9 && temp.length() <= 11))
			throw new ValidatorException(new FacesMessage(
					"Imei must has from 9 to 11 numbers"));
	}

	public void validatePort(FacesContext fc, UIComponent c, Object value) {

	}

	public void validateIpAddress(FacesContext fc, UIComponent c, Object value) {
		String IPADDRESS_PATTERN = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$";
		String temp = value.toString();
		if (!(temp.matches(IPADDRESS_PATTERN)))
			throw new ValidatorException(new FacesMessage("Invalid ip address"));
	}

}
